package com.etms.service;

import com.etms.mapper.SupplierMapper;
import com.etms.pojo.Supplier;
import com.etms.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierService {

    @Autowired
    SupplierMapper supplierMapper;


    public List<Supplier> findSupplierList() {

        return supplierMapper.findList();
    }

    public Supplier findById(Integer gongId) {
        return supplierMapper.findById(gongId);
    }

    public Result findAll() {
        List<Supplier> supplierList = findSupplierList();
        return Result.successed(supplierList);
    }

    public Result findSuById(Integer id) {
        Supplier byId = findById(id);
        return Result.successed(byId);
    }

    public Result delSuById(Integer id) {
        int cnt=supplierMapper.delById(id);
        if(cnt==0){
            return Result.failed("删除失败!");
        }
        return Result.successed("删除成功",null);
    }

    public Result updateOrAdd(Supplier supplier) {
        if (supplier.getId()==0||supplier.getId()==null){//新增
            int cnt=supplierMapper.addOne(supplier);
            if(cnt==0)return Result.failed("新增失败");

            return Result.successed("新增成功!",null);
        }
        //否则是更新
        int cnt=supplierMapper.updateOne(supplier);
        if (cnt==0)return Result.failed("更新失败");
        return Result.successed("更新成功",null);
    }
}
